/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: Consumer
 * Author:   666
 * Date:     2022/4/21 0:26
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.mashibing.hello;

import com.mashibing.util.RabbitMQConnectionUtil;
import com.mashibing.workqueues.Publisher;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

/**
 * @Classname Consumer
 * @Description TODO
 * @Date 2022/4/21 0:26
 * @Created by 666
 */
public class Consumer {

    @Test
    public void consumer() throws Exception {
        //1、创建connection
        Connection connection = RabbitMQConnectionUtil.getConnection();

        //2、创建管道channels
        Channel channel = connection.createChannel();
        //3、构建队列
        channel.queueDeclare(com.mashibing.workqueues.Publisher.QUEUE_NAME, false, false, false, null);
        //4、接受消息
        DefaultConsumer callback =new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消費者獲取到消息"+new String(body,"UTF-8"));
            }
        };
        channel.basicConsume(Publisher.QUEUE_NAME, true, callback);
        System.out.println("开始监听");
        System.in.read();
    }
}
